{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "v2.ipynb",
      "private_outputs": true,
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "QIi6fQudR7ly"
      },
      "source": [
        "!pip install plotly==4.8\r\n",
        "!pip install pandas_ta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dEGmcDADSDCe"
      },
      "source": [
        "import plotly.express as px\r\n",
        "import pandas_ta as ta\r\n",
        "import pandas as pd\r\n",
        "import numpy as np\r\n",
        "import random\r\n",
        "\r\n",
        "#graphs all columns for x, uses df.index as y\r\n",
        "def graph_stock(df, title):\r\n",
        "  #time_column is a string\r\n",
        "\r\n",
        "  pd.options.plotting.backend = \"plotly\"\r\n",
        "  #fig = px.line(df, x='time', y=['price', 'price_2'])\r\n",
        "  fig = px.line(df, x=df.index, y=df.columns, title=title)\r\n",
        "  fig.show()\r\n",
        "\r\n",
        "def graph_OCHL(df_OCHL, title):\r\n",
        "  #fig_1 = px.line(df, x=df.index, y=df.columns, title=title)\r\n",
        "  fig_2 = go.Figure(data=go.Ohlc(x=df_OCHL.index,\r\n",
        "                      open=df_OCHL['open'],\r\n",
        "                      high=df_OCHL['high'],\r\n",
        "                      low=df_OCHL['low'],\r\n",
        "                      close=df_OCHL['close']))\r\n",
        "  fig_2.update(layout_xaxis_rangeslider_visible=False)\r\n",
        "  fig_2.show()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hCtuuW_pw7gW"
      },
      "source": [
        "from scipy.stats import skewnorm\r\n",
        "\r\n",
        "def simulate_stock(initial_price, drift, volatility, trend, days):\r\n",
        "\r\n",
        "  def create_pdf(sd, mean, alfa):\r\n",
        "    #invertire il segno di alfa\r\n",
        "    x = skewnorm.rvs(alfa, size=1000000) \r\n",
        "    def calc(k, sd, mean):\r\n",
        "      return (k*sd)+mean\r\n",
        "    x = calc(x, sd, mean) #standard distribution\r\n",
        "\r\n",
        "    #graph pdf\r\n",
        "    #pd.DataFrame(x).hist(bins=100)\r\n",
        "\r\n",
        "    #pick one random number from the distribution\r\n",
        "    #formally I would use cdf, but I just have to pick randomly from the 1000000 samples\r\n",
        "    #np.random.choice(x)\r\n",
        "    return x\r\n",
        "\r\n",
        "  def create_empty_df(days):\r\n",
        "    #creare un empty DataFrame con le date\r\n",
        "    empty = pd.DatetimeIndex(\r\n",
        "        pd.date_range(\"2020-01-01\", periods=days, freq=\"D\")\r\n",
        "    )\r\n",
        "    empty = pd.DataFrame(empty)\r\n",
        "    #si tagliano ore, minuti, secondi\r\n",
        "    empty\r\n",
        "\r\n",
        "    #si tagliano ore, minuti, secondi\r\n",
        "    empty.index = [str(x)[0:empty.shape[0]] for x in list(empty.pop(0))]\r\n",
        "    empty\r\n",
        "\r\n",
        "    #final dataset con values\r\n",
        "    stock = pd.DataFrame([x for x in range(0, empty.shape[0])])\r\n",
        "    stock.index = empty.index\r\n",
        "    return stock\r\n",
        "\r\n",
        "  #skeleton\r\n",
        "  stock = create_empty_df(days)\r\n",
        "\r\n",
        "  #initial price\r\n",
        "  stock[0][0] = initial_price\r\n",
        "\r\n",
        "  #create entire stock DataFrame\r\n",
        "  x = create_pdf(volatility, drift, trend)\r\n",
        "  for _ in range(1, stock.shape[0]):\r\n",
        "    stock.iloc[_] = stock.iloc[_-1]*(1+np.random.choice(x))\r\n",
        "  \r\n",
        "  stock.index = pd.DatetimeIndex(stock.index)\r\n",
        "  return stock\r\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P-Oo6Gfa6537"
      },
      "source": [
        "#normal distribution for difference between low and high\r\n",
        "def create_pdf(sd, mean, alfa):\r\n",
        "  #invertire il segno di alfa\r\n",
        "  x = skewnorm.rvs(alfa, size=1000000) \r\n",
        "  def calc(k, sd, mean):\r\n",
        "    return (k*sd)+mean\r\n",
        "  x = calc(x, sd, mean) #standard distribution\r\n",
        "\r\n",
        "  #graph pdf\r\n",
        "  #pd.DataFrame(x).hist(bins=100)\r\n",
        "\r\n",
        "  #pick one random number from the distribution\r\n",
        "  #formally I would use cdf, but I just have to pick randomly from the 1000000 samples\r\n",
        "  #np.random.choice(x)\r\n",
        "  return x\r\n",
        "\r\n",
        "ran = create_pdf(0.1, 0.2, 0)\r\n",
        "np.random.choice(ran)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "osDymcDDqU8R"
      },
      "source": [
        "def OCHL(group_values):\r\n",
        "  min_ = min(group_values)\r\n",
        "  max_ = max(group_values)\r\n",
        "  range = max_ - min_\r\n",
        "  open = min_+range*random.random()\r\n",
        "  close = min_+range*random.random()\r\n",
        "  return min_, max_, open, close"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zZL0AmwVh4Bj"
      },
      "source": [
        "import plotly.graph_objects as go\r\n",
        "import pandas as pd\r\n",
        "\r\n",
        "df = simulate_stock(1000, 0, 0.01, 0, 8760)\r\n",
        "\r\n",
        "df_ = list()\r\n",
        "#df.groupby(np.arange(len(df))//24).apply(OCHL) non funziona\r\n",
        "#sarebbe il modo corretto, ma devo creare un nuovo df da 0\r\n",
        "for a, b in df.groupby(np.arange(len(df))//24):\r\n",
        "  group_values = np.array(b.values).flatten()\r\n",
        "  low, high, open, close = OCHL(group_values)\r\n",
        "  df_.append([low, high, open, close])\r\n",
        "\r\n",
        "#\r\n",
        "df_OCHL = pd.DataFrame(df_, index=pd.Series(pd.date_range(\"2020-01-01\", periods=365, freq=\"D\")), columns=['low', 'high', 'open', 'close'])\r\n",
        "\r\n",
        "#graph\r\n",
        "graph_stock(df, \"\")\r\n",
        "fig = go.Figure(data=go.Ohlc(x=df_OCHL.index,\r\n",
        "                    open=df_OCHL['open'],\r\n",
        "                    high=df_OCHL['high'],\r\n",
        "                    low=df_OCHL['low'],\r\n",
        "                    close=df_OCHL['close']))\r\n",
        "fig.update(layout_xaxis_rangeslider_visible=False)\r\n",
        "fig.show()"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}